package com.wms.presenters;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gatech.wms.R;
import com.wms.helper.DialogFactory;
import com.wms.model.AdminModel;
import com.wms.model.LoginModel;
import com.wms.assets.User;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;


public class AdminActivity extends WMSActivity {
	
	private Spinner dropdown;
	AdminModel model;
	DialogFactory d;
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin_screen);
		model = new AdminModel();
		d = new DialogFactory(this);
		setupSpinner();
		setupButton();
	}
	
	private void setupSpinner() {
		dropdown = (Spinner) findViewById(R.id.dropdown);
		List<User> suspended = null;
		List<String> displayUsers;
		try {
			suspended = (new LoginModel()).getSuspendedUserList();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		displayUsers = new ArrayList<String>();
		for (User user: suspended){
			displayUsers.add(user.getUsername());
		}
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, displayUsers);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		dropdown.setAdapter(dataAdapter);
	}

	private void setupButton(){
		Button deleteUser = (Button)findViewById(R.id.button4);  
		final EditText userEntry = (EditText)findViewById(R.id.editText1);
		deleteUser.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				String username = userEntry.getText().toString();
				try {
					model.removeUser(username);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					d.alertOK("Error in SQL retrieval", "Error");
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					d.alertOK("Error in JDBC initialization.", "Error");
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					d.alertOK("Error in whaaa? : " + e.getMessage(), "Error");
				} catch (ClassNotFoundException e) {
					d.alertOK("Error in JDBC initialization.", "Error");
				}
			}
			
		});
		
		Button unblockUser = (Button)findViewById(R.id.button1);  
		
		unblockUser.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String username = userEntry.getText().toString();
				try {
					model.activateUser(username);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					d.alertOK("Error in SQL retrieval", "Error");
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					d.alertOK("Error in JDBC initialization.", "Error");
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					d.alertOK("Error in whaaa? : " + e.getMessage(), "Error");
				} catch (ClassNotFoundException e) {
					d.alertOK("Error in JDBC initialization.", "Error");
				}
			}
		});
		
		Button makeAdmin = (Button)findViewById(R.id.button2);  
		
		makeAdmin.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				String username = userEntry.getText().toString();
				
				try {
					model.setAdmin(username);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					d.alertOK("Error in SQL retrieval", "Error");
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					d.alertOK("Error in JDBC initialization.", "Error");
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					d.alertOK("Error in whaaa? : " + e.getMessage(), "Error");
				} catch (ClassNotFoundException e) {
					d.alertOK("Error in JDBC initialization.", "Error");
				}
				
			}
			
		});
	}

}
